package com.util;

import java.math.BigDecimal;

/**
 *	@description 精确保留
 *	@author 
 * 	@since 
 *	@version 1.0*/
public class BigDecimalUtil {
	/**@description 百分比精确到小数点两位a/b
	 */
	public static String bigDecimal(long a, long b){
		StringBuffer ab = new StringBuffer();
		ab.append(a*100);
		StringBuffer sb = new StringBuffer();
		sb.append(b);
		if(b==0){
			return "1";
		}else{
			StringBuffer ssbb = new StringBuffer();
			Object ob = new BigDecimal(ab.toString()).divide(new BigDecimal(sb.toString()), 2, BigDecimal.ROUND_HALF_UP);
			ssbb.append(ob);
			return ssbb.toString()+"%";
		}
	}
	/**@description 将一数变成以万元为单位，并保留两位有效数字
	 */
	public static String tenThousand(double d){
		Object ob = new BigDecimal(d).divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
		StringBuffer sb = new StringBuffer();
		sb.append(ob);
		return sb.toString();
	}
	/**@description s/l保留到整位
	 */
	public static String round(String str,long l){
		double d = Double.valueOf(str)*10000;
		if(l==0){
			return "1";
		}else{
			Object ob = new BigDecimal(d).divide(new BigDecimal(l), 0, BigDecimal.ROUND_HALF_UP);
			StringBuffer sb = new StringBuffer();
			sb.append(ob);
			return sb.toString();
		}
	}
	/**@description a/b百分比精确到小数点两位
	 */
	public static String getDD(double a,double b){
		if(b==0){
			return "1";
		}else{
			Object ob = new BigDecimal(a*100).divide(new BigDecimal(b),2, BigDecimal.ROUND_HALF_UP);
			StringBuffer sb = new StringBuffer();
			sb.append(ob);
			return sb.toString()+"%";
		}
	}
	/**@description a/b精确到小数点两位
	 */
	public static String getDl(double a,long b){
		if(b==0){
			return "1";
		}else{
			Object ob = new BigDecimal(a).divide(new BigDecimal(b),2, BigDecimal.ROUND_HALF_UP);
			StringBuffer sb = new StringBuffer();
			sb.append(ob);
			return sb.toString();
		}
	}
}
